/**
 *
 */
package pt.mleiria.app.web.bean;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import javax.ejb.EJB;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import pt.mleiria.app.web.utils.Navigator;
import pt.mleiria.bo.crypto.CryptoEJB;
import pt.mleiria.bo.mother.MotherVoiceEJB;

/**
 * @author NB13712
 *
 */
@ManagedBean(name = "masterBean")
@SessionScoped
public class MasterBean {

    @EJB
    private CryptoEJB cryptoEjb;
    @EJB
    private MotherVoiceEJB mvEjb;
    /**
     *
     * @return
     */
    public CryptoEJB getCryptoEjb() {
        return cryptoEjb;
    }
    
    /**
     *
     * @return
     */
    public MotherVoiceEJB getMotherVoiceEjb() {
        return mvEjb;
    }

    final private ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    protected int index;
    public Map<String, Integer> menuList = new TreeMap<String, Integer>();

    public MasterBean() {
        mvEjb = new MotherVoiceEJB();
        cryptoEjb = new CryptoEJB();
        for (Navigator nav : Navigator.values()) {
            menuList.put(nav.getMenuName(), nav.getValue());
            //Logger.getLogger(MasterBean.class).debug("["+nav.getMenuName()+";"+ nav.getValue()+"]");
        }

    }

    public static Logger getLogger(Class clazz) {
        Logger.getLogger(clazz).setLevel(Level.INFO);
        return Logger.getLogger(clazz);
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String redirect(int activeIndex) {
        //Logger.getLogger(MasterBean.class).debug("[Active Index:"+index+"]");
        switch (activeIndex) {
            case 0:
                index = Navigator.Dashboard.getValue();
                return Navigator.Dashboard.getLnk();
            case 1:
                index = Navigator.LivingRoom.getValue();
                return Navigator.LivingRoom.getLnk();
            case 2:
                index = Navigator.KikaRoom.getValue();
                return Navigator.KikaRoom.getLnk();
            case 3:
                index = Navigator.ZinhaRoom.getValue();
                return Navigator.ZinhaRoom.getLnk();
            case 4:
                index = Navigator.KidosRoom.getValue();
                return Navigator.KidosRoom.getLnk();
            case 5:
                index = Navigator.ParentsRoom.getValue();
                return Navigator.ParentsRoom.getLnk();
            case 6:
                index = Navigator.PlayGround.getValue();
                return Navigator.PlayGround.getLnk();
            //return "playground";
            default:
                index = Navigator.Dashboard.getValue();
                return Navigator.Dashboard.getLnk();
        }
    }

    public Map<String, Integer> getMenuList() {
        return menuList;
    }

    public void setMenuList(Map<String, Integer> menuList) {
        this.menuList = menuList;
    }

    public void setToSession(final String key, final Object value) {
        try {
            final Map<String, Object> sessionMap = externalContext.getSessionMap();
            sessionMap.put(key, value);
        } catch (NullPointerException e) {
            logout();
        }
    }

    public Object getFromSession(final String key) {
        try {
            final Map<String, Object> sessionMap = externalContext.getSessionMap();
            return (Object) sessionMap.get(key);

        } catch (NullPointerException e) {
            logout();
        }
        return null;
    }

    public String logout() {
        try {
            externalContext.invalidateSession();
            externalContext.redirect("/adaline");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    public String[] getAlphabetLetters(){
        return cryptoEjb.getAlphabetLetters();
    }

}
